package com.apikey.auth.entity;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * API Key申请记录实体类
 * 表示用户的API Key申请信息
 *
 * @author API Key Framework
 * @version 1.0
 */
@Entity
@Table(name = "applications", indexes = {
        @Index(name = "idx_applications_phone", columnList = "phone_number"),
        @Index(name = "idx_applications_status", columnList = "status"),
        @Index(name = "idx_applications_apply_time", columnList = "apply_time"),
        @Index(name = "idx_applications_fingerprint", columnList = "fingerprint")
})
public class Application {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "phone_number", nullable = false, length = 20)
    private String phoneNumber;

    @Column(name = "verify_password", length = 255)
    private String verifyPassword;

    @Column(name = "fingerprint", nullable = false, length = 255)
    private String fingerprint;

    @Column(name = "status", nullable = false, length = 20)
    private String status = "PENDING";

    @Column(name = "apply_time", nullable = false)
    private LocalDateTime applyTime;

    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    @Column(name = "reviewer", length = 100)
    private String reviewer;

    @Column(name = "remark", columnDefinition = "TEXT")
    private String remark;

    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    /**
     * 申请状态枚举
     */
    public enum Status {
        PENDING("PENDING", "待审核"),
        APPROVED("APPROVED", "已通过"),
        REJECTED("REJECTED", "已拒绝");

        private final String code;
        private final String description;

        Status(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 默认构造函数
     */
    public Application() {
        this.applyTime = LocalDateTime.now();
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 构造函数
     *
     * @param phoneNumber 手机号
     * @param fingerprint 浏览器指纹
     */
    public Application(String phoneNumber, String fingerprint) {
        this();
        this.phoneNumber = phoneNumber;
        this.fingerprint = fingerprint;
    }

    /**
     * 构造函数
     *
     * @param phoneNumber    手机号
     * @param verifyPassword 验证密码
     * @param fingerprint    浏览器指纹
     */
    public Application(String phoneNumber, String verifyPassword, String fingerprint) {
        this(phoneNumber, fingerprint);
        this.verifyPassword = verifyPassword;
    }

    /**
     * 更新时间戳
     */
    @PreUpdate
    public void preUpdate() {
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 审核申请
     *
     * @param status   审核状态
     * @param reviewer 审核人
     * @param remark   审核备注
     */
    public void review(String status, String reviewer, String remark) {
        this.status = status;
        this.reviewer = reviewer;
        this.remark = remark;
        this.reviewTime = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 检查是否已审核
     *
     * @return 是否已审核
     */
    public boolean isReviewed() {
        return !Status.PENDING.getCode().equals(this.status);
    }

    /**
     * 检查是否审核通过
     *
     * @return 是否审核通过
     */
    public boolean isApproved() {
        return Status.APPROVED.getCode().equals(this.status);
    }

    /**
     * 检查是否被拒绝
     *
     * @return 是否被拒绝
     */
    public boolean isRejected() {
        return Status.REJECTED.getCode().equals(this.status);
    }

    // Getter和Setter方法

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getVerifyPassword() {
        return verifyPassword;
    }

    public void setVerifyPassword(String verifyPassword) {
        this.verifyPassword = verifyPassword;
    }

    public String getFingerprint() {
        return fingerprint;
    }

    public void setFingerprint(String fingerprint) {
        this.fingerprint = fingerprint;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public LocalDateTime getApplyTime() {
        return applyTime;
    }

    public void setApplyTime(LocalDateTime applyTime) {
        this.applyTime = applyTime;
    }

    public LocalDateTime getReviewTime() {
        return reviewTime;
    }

    public void setReviewTime(LocalDateTime reviewTime) {
        this.reviewTime = reviewTime;
    }

    public String getReviewer() {
        return reviewer;
    }

    public void setReviewer(String reviewer) {
        this.reviewer = reviewer;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public String toString() {
        return "Application{" +
                "id=" + id +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", fingerprint='" + fingerprint + '\'' +
                ", status='" + status + '\'' +
                ", applyTime=" + applyTime +
                ", reviewTime=" + reviewTime +
                ", reviewer='" + reviewer + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}